package com.web.controller;

import com.core.dto.FileUploadDTO;
import com.core.dto.GoodDTO;
import com.core.dto.UserDTO;
import com.core.fileDownload.FileAccess;
import com.core.model.lxf.Good;
import com.core.shared.ExportExcel;
import com.core.shared.ImageUtils;
import com.core.shared.ResponseHandle;
import com.core.websocket.WebSocket;
import com.service.LxfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-3
 * Time: 下午4:40
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/index")
public class TestController {
    @Autowired
    private LxfService lxfService;

    @RequestMapping(value = "get_all_ips", method = RequestMethod.GET)
    @ResponseBody
    public Object show() {
        return WebSocket.getAllUserIp();
    }

    @RequestMapping(value = "file_upload", method = RequestMethod.POST)
    private String fileUpload(FileUploadDTO fileUploadDTO) {
        lxfService.saveOrUpdateFile(fileUploadDTO);
        MultipartFile file;
        return "front/file";
    }

    @RequestMapping(value = "response")
    public String response(HttpServletResponse response) {
        ResponseHandle.optimizationResponse(response);
        return "success";
    }

    @RequestMapping(value = "upload")
    public void test(MultipartFile file) {
        lxfService.upload(file);
    }

    @RequestMapping(value = "download")
    public void download(HttpServletResponse response, HttpServletRequest request) {
//        lxfService.download(response);
        FileAccess fileAccess = new FileAccess();
        try {
            fileAccess.download(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "export")
    public void export(HttpServletResponse response) {
        Good g1 = new Good("第一行", "21");
        Good g2 = new Good("第二行", "21");
        Good g3 = new Good("第三行", "21");
        String filename = "ExportExcelTest.xlsx";
        String title = filename.replace(".xlsx", "");
        String[] rowsName = new String[]{"序号", "编号", "名称"};// list数据的列名
        List<Object[]> datalist = new ArrayList<>();
        List<Good> list_ygd = new ArrayList<>();//模拟一个数据集
        list_ygd.add(g1);
        list_ygd.add(g2);
        list_ygd.add(g3);
        for (int i = 0; i < list_ygd.size(); i++) {
            Object[] objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = list_ygd.get(i).name();
            objs[2] = list_ygd.get(i).age();
            datalist.add(objs);
        }
        try {
            ExportExcel ex = new ExportExcel(title, rowsName, datalist);
            ex.export(response, filename);  // 导出
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "all_good", method = RequestMethod.GET)
    @ResponseBody
    public Object allStudent() {
        List<GoodDTO> goods = lxfService.allStudent();
        return goods;
    }

    @RequestMapping(value = "save_good", method = RequestMethod.POST)
    @ResponseBody
    public Object saveGood(GoodDTO goodDTO) {
        return lxfService.saveGood(goodDTO);
    }

    @RequestMapping(value = "del_good/{goodGuid}", method = RequestMethod.GET)
    @ResponseBody
    public Object delGood(@PathVariable String goodGuid) {
        return lxfService.delGood(goodGuid);
    }

    @RequestMapping(value = "update_good", method = RequestMethod.POST)
    @ResponseBody
    public Object updataGood(GoodDTO goodDTO) {
        return lxfService.updateGood(goodDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelAndView springForm() {
        return new ModelAndView("front/formTag", "userDTO", new UserDTO());
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String springFormSubmit(@Valid UserDTO userDTO, Errors errors) {
        if (errors.hasErrors()) {
            return "front/formTag";
        }
        System.out.println(userDTO.getGuid());
        System.out.println(userDTO.getUsername());
        System.out.println(userDTO.getPassword());
        System.out.println(userDTO.getNickname());
        return "front/formTag";
    }

    @RequestMapping(value = "model/test")
    public String modelTest() {
//        lxfService.add(new GoodDTO());
        return "thymeleaf";
    }

    @RequestMapping(value = "getIp")
    public String getIp(HttpServletRequest request, Model model) {
        model.addAttribute("ip", WebSocket.getIpAddress(request));
        return "test";
    }

}
